var mongoose = require('mongoose');
require('../model/customer.model');
var customerModel = mongoose.model('Customer');

module.exports={
    addCustomer:addCustomer,
    findByCustomerName:findByCustomerName,
    findByCustomerNameAndPwd:findByCustomerNameAndPwd,
    updateCustomer:updateCustomer,
    findByCustomerId:findByCustomerId,
    addAddress,addAddress,
    deleteAddress:deleteAddress,
    findAddressList:findAddressList
};

/**
 * find user by ids
 * @param userid
 */
function addCustomer(customerInfo,callBack){
    customerModel.create(customerInfo,function(err,doc){
        callBack(err,doc);
    });
}


function findByCustomerName(cName,callBack){
    customerModel.findOne({'cName':cName},function(err,doc){
        callBack(err,doc);
    });
}

function findByCustomerNameAndPwd(cName,cpwd,callBack){
    customerModel.findOne({'cName':cName,'cpwd':cpwd},function(err,doc){
        callBack(err,doc);
    });
}

function updateCustomer(customerInfo,callBack){
    customerModel.update({'_id':customerInfo._id},customerInfo,function (err,doc) {
        callBack(err,doc);
    });
}

function findByCustomerId(_id,callBack){
    customerModel.findOne({'_id':_id},function(err,doc){
        callBack(err,doc);
    });
}

function addAddress(customerId,address,callBack){
    customerModel.update({'_id':customerId},{$push:{'address':{'name':address}}},function(err,doc){
        callBack(err,doc);
    });
}

function deleteAddress(customerId,addressId,callBack){
    customerModel.update({'_id':customerId},{$pull:{'address':{'_id':addressId}}},function(err,doc){
        callBack(err,doc);
    });
}


function findAddressList(customerId,callBack){
    customerModel.findOne({'_id':customerId},{address:1},function(err,doc){
        callBack(err,doc);
    });
}

// function addOrder(orderId,customerId,callBack){
//     customerModel.update({'_id':customerId},{$push:{'order':orderId}},function (err,doc){
//         callBack(err,doc);
//     });
// }
